﻿using CSharpDay10__Entity_Framwork_.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CSharpDay10__Entity_Framwork_.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            //using creates its own scope, so we have to declare the variable above it
            List<Movie> movies;
            //Use the database window within the curly braces, and then destroy it
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                //our list is equal to all of the movies on the "Movies" table
                movies = db.Movies.ToList();
            }
            return View(movies);
        }

        public ActionResult AddMovie()
        {
            return View(new Movie());
        }

        [HttpPost]
        public ActionResult AddMovie(Movie model)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                db.Movies.Add(model);
                //We have to save any changes that we make to the info in our database
                db.SaveChanges();
            }
            return RedirectToAction("Index");
        }


        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}